cmake_minimum_required(VERSION 3.10)
project(HttpLib VERSION 1.0.0)

# Set C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -O2")

# Find required packages
find_package(Threads REQUIRED)
find_package(CURL REQUIRED)

# Source files
set(COMMON_SOURCES
    common/poller.cpp
    common/Packet.cpp
    common/timer.cpp
    common/decoder.cpp
    common/global.cpp
    common/CCReactor.cpp
)

set(HTTP_SOURCES
    http/HttpData.cpp
    http/HttpDecoder.cpp
    http/HttpHandler.cpp
    http/HttpListener.cpp
    http/HttpServer.cpp
    http/http_parser.c
)

# Include CurlClient sources
set(CURL_SOURCES
    curl/CurlClient.cpp
)

# Include directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/common)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/http)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/curl)
include_directories(${CURL_INCLUDE_DIRS})

# Create static library
add_library(httplib STATIC
    ${COMMON_SOURCES}
    ${HTTP_SOURCES}
    ${CURL_SOURCES}
)

# Link libraries
target_link_libraries(httplib
    ${CURL_LIBRARIES}
    Threads::Threads
)

# Create separate test executables
add_executable(http_server_test
    tests/test_server.cpp
)

add_executable(http_client_test
    tests/test_client.cpp
)

add_executable(http_performance_test
    tests/test_performance.cpp
)

add_executable(concurrent_player_test
    tests/concurrent_player_test.cpp
)

add_executable(simple_concurrent_test
    tests/simple_concurrent_test.cpp
)

add_executable(basic_curl_test
    tests/basic_curl_test.cpp
)

add_executable(thread_test
    tests/thread_test.cpp
)

add_executable(simple_player_test
    tests/simple_player_test.cpp
)

add_executable(integrated_concurrent_test
    tests/integrated_concurrent_test.cpp
)

add_executable(simple_server_client_test
    tests/simple_server_client_test.cpp
)

target_link_libraries(http_server_test
    httplib
    ${CURL_LIBRARIES}
    Threads::Threads
)

target_link_libraries(http_client_test
    httplib
    ${CURL_LIBRARIES}
    Threads::Threads
)

target_link_libraries(http_performance_test
    httplib
    ${CURL_LIBRARIES}
    Threads::Threads
)

target_link_libraries(concurrent_player_test
    httplib
    ${CURL_LIBRARIES}
    Threads::Threads
)

target_link_libraries(simple_concurrent_test
    httplib
    ${CURL_LIBRARIES}
    Threads::Threads
)

target_link_libraries(basic_curl_test
    httplib
    ${CURL_LIBRARIES}
    Threads::Threads
)

target_link_libraries(thread_test
    httplib
    ${CURL_LIBRARIES}
    Threads::Threads
)

target_link_libraries(simple_player_test
    httplib
    ${CURL_LIBRARIES}
    Threads::Threads
)

target_link_libraries(integrated_concurrent_test
    httplib
    ${CURL_LIBRARIES}
    Threads::Threads
)

target_link_libraries(simple_server_client_test
    httplib
    ${CURL_LIBRARIES}
    Threads::Threads
)

# Simple async test (removed - file was deleted)
# add_executable(simple_async_test
#     simple_async_test.cpp
# )

# target_link_libraries(simple_async_test
#     httplib
#     ${CURL_LIBRARIES}
#     Threads::Threads
# )

# Create example executables
add_executable(http_server_example
    examples/http_server_example.cpp
)

target_link_libraries(http_server_example
    httplib
    ${CURL_LIBRARIES}
    Threads::Threads
)

add_executable(http_client_example
    examples/http_client_example.cpp
)

target_link_libraries(http_client_example
    httplib
    ${CURL_LIBRARIES}
    Threads::Threads
)

# Install targets
install(TARGETS httplib
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
    RUNTIME DESTINATION bin
)

install(DIRECTORY common/ http/ curl/
    DESTINATION include/httplib
    FILES_MATCHING PATTERN "*.h"
)

# Testing
enable_testing()
add_test(NAME http_server_test COMMAND http_server_test)
add_test(NAME http_client_test COMMAND http_client_test)
add_test(NAME http_performance_test COMMAND http_performance_test)

# Compiler-specific settings
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
endif()

# Package configuration
set(CPACK_PACKAGE_NAME "HttpLib")
set(CPACK_PACKAGE_VERSION "1.0.0")
set(CPACK_PACKAGE_DESCRIPTION "C++ HTTP Library with Server and Client")
set(CPACK_PACKAGE_CONTACT "Your Name <your.email@example.com>")
include(CPack)